/*
 *  LLFileUtils_ios.mm
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "lid_lib/src/file_managment/LLFileUtils.h"

#import <Foundation/Foundation.h>

#include <stdio.h>

using namespace std;

namespace lidlib {
    
/////////////////////////////////////////////////////
//
/** CEFileUtils 
 * 
 */
/////////////////////////////////////////////////////

std::vector<std::string> LLFileUtils::getFileList(const std::string& fullDirectoryPath)
{
    std::vector<std::string> result;
    
    NSString* strPath = [NSString stringWithCString:fullDirectoryPath.c_str() encoding:NSASCIIStringEncoding];  
    if (![[NSFileManager defaultManager] fileExistsAtPath:strPath])
        return result;
    
    BOOL isDir = NO;
    NSArray* content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:strPath error:nil];
    for (NSString* item in content) 
    {
        NSString* itemPath = [strPath stringByAppendingPathComponent:item];
        if ([[NSFileManager defaultManager] fileExistsAtPath:itemPath isDirectory:&isDir] && !isDir) 
            result.push_back([item UTF8String]);
    }
    
    return result;
}

std::vector<std::string> LLFileUtils::getDirectoryList(const std::string& fullDirectoryPath)
{
    std::vector<std::string> result;

    NSString* strPath = [NSString stringWithCString:fullDirectoryPath.c_str() encoding:NSASCIIStringEncoding];  
    if (![[NSFileManager defaultManager] fileExistsAtPath:strPath])
        return result;
 
    BOOL isDir = NO;
    NSArray* content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:strPath error:nil];
    for (NSString* item in content) 
    {
        NSString* itemPath = [strPath stringByAppendingPathComponent:item];
        if ([[NSFileManager defaultManager] fileExistsAtPath:itemPath isDirectory:&isDir] && isDir) 
             result.push_back([item UTF8String]);
    }
    
    return result;
}
    
    
string LLFileUtils::getWriteablePath()
{
    // save to document folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    std::string strRet = [documentsDirectory UTF8String];
    strRet.append("/");
    return strRet;
}

bool  LLFileUtils::isFileExists(const std::string& fileName)
{
    FILE *fp = fopen(fileName.c_str(), "rb");
    if(fp == NULL)
        return false;
    fclose(fp);
    return true;
}

unsigned char*  LLFileUtils::getFileData(const std::string& fileName, unsigned long* pSize)
{
    // read the file from hardware
    FILE *fp = fopen(fileName.c_str(), "rb");
    if (fp == NULL)
        return NULL;
    
    fseek(fp,0,SEEK_END);
    *pSize = ftell(fp);
    
    fseek(fp,0,SEEK_SET);
    unsigned char* pBuffer = new unsigned char[*pSize];
    *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
    fclose(fp);
    
    return pBuffer;
}

std::string  LLFileUtils::fullPathFromRelativePath(const std::string& relativePath)
{
    
    // do not convert an absolute path (starting with '/')
    NSString *relPath = [NSString stringWithUTF8String: relativePath.c_str()];
    NSString *fullpath = nil;
	
	// only if it is not an absolute path
	if( ! [relPath isAbsolutePath] )
	{
		NSString *file = [relPath lastPathComponent];
		NSString *imageDirectory = [relPath stringByDeletingLastPathComponent];
		
		fullpath = [[NSBundle mainBundle] pathForResource:file
												   ofType:nil
											  inDirectory:imageDirectory];
	}
	
	if (fullpath == nil)
		fullpath = relPath;

    
    return string([fullpath UTF8String]);
}

std::string  LLFileUtils::fullPathFromRelativeFile(const std::string& filename, const std::string& relativeFile)
{
    std::string fullFile = fullPathFromRelativePath(relativeFile);
    std::string ret = fullFile.substr(0, fullFile.find_last_of("/") + 1);
    ret += filename;
    
    return ret;
}

    
    
//    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];	
//    _resourceFolder = [[documentsPath stringByAppendingPathComponent:RESOURCE_FOLDER_NAME] retain];
//		_bundleResourceFolder = [[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:RESOURCE_FOLDER_NAME] retain];    
    
}; // end namespace cocosext
